﻿
using System.Text;
using NLog;
using NLog.Config;
using NLog.Targets;

namespace Hl.Core.Helper;

public class NLogHelper
{
    public static Logger logger { get; private set; }
    static NLogHelper()
    {
        var currentConfiguration = new LoggingConfiguration();

        // Step 2. Create targets and add them to the configuration 
        //var consoleTarget = new ColoredConsoleTarget();
        //CurrentConfiguration.AddTarget("console", consoleTarget);

        var fileTarget = new FileTarget();
        currentConfiguration.AddTarget("file", fileTarget);

        // Step 3. Set target properties 
        //consoleTarget.Layout = @"${date:format=HH\\:MM\\:ss} ${logger} ${message}";
        fileTarget.FileName = "${basedir}/logs/${shortdate}.log";
        fileTarget.Layout = "${longdate} ${level:uppercase=true} ${event-context:item=Action} ${message} ${event-context:item=Amount} ${stacktrace}";
        fileTarget.ArchiveAboveSize = 1024 * 1024 * 5;
        fileTarget.ArchiveEvery = FileArchivePeriod.Day;
        fileTarget.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence;
        fileTarget.ConcurrentWrites = true;
        fileTarget.KeepFileOpen = false;
        fileTarget.CreateDirs = true;
        fileTarget.Encoding = Encoding.UTF8;

        // Step 4. Define rules
        //var rule1 = new LoggingRule("*", LogLevel.Debug, consoleTarget);
        //CurrentConfiguration.LoggingRules.Add(rule1);

        var rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
        currentConfiguration.LoggingRules.Add(rule2);

        LogManager.Configuration = currentConfiguration;
        
        //var nl = new NlogLoggingConfiguration();
        //var nlogConfig = $"{System.AppDomain.CurrentDomain.BaseDirectory}{CoreDefine.NlogConfigName}";
        //if (!File.Exists(nlogConfig))
        //{
        //    var nlogConfigTxt = CoreDefine.NlogConfig;
        //    File.WriteAllText(nlogConfig, nlogConfigTxt);
        //}
        logger = NLog.LogManager.GetCurrentClassLogger();
    }
    #region Debug

    public static void Debug(string msg, params object[] args)
    {
        logger.Debug(msg, args);
    }

    public static void Debug(string msg, Exception err)
    {
        logger.Debug(err, msg);
    }

    #endregion

    #region Info

    public static void Info(string msg, params object[] args)
    {
        logger.Info(msg, args);
    }

    public static void Info(string msg, Exception err)
    {
        logger.Info(err, msg);
    }

    #endregion

    #region Warn

    public static void Warn(string msg, params object[] args)
    {
        logger.Warn(msg, args);
    }

    public static void Warn(string msg, Exception err)
    {
        logger.Warn(err, msg);
    }

    #endregion

    #region Trace

    public static void Trace(string msg, params object[] args)
    {
        logger.Trace(msg, args);
    }

    public static void Trace(string msg, Exception err)
    {
        logger.Trace(err, msg);
    }

    #endregion

    #region Error

    public static void Error(string msg, params object[] args)
    {
        logger.Error(msg, args);
    }

    public static void Error(string msg, Exception err)
    {
        logger.Error(err, msg);
    }

    #endregion

    #region Fatal

    public static void Fatal(string msg, params object[] args)
    {
        logger.Fatal(msg, args);
    }

    public static void Fatal(string msg, Exception err)
    {
        logger.Fatal(err, msg);
    }

    #endregion
}